package servlet.admin;

import entity.Rules;
import net.sf.json.JSONObject;
import service.admin.RulesService;
import service.admin.impl.RulesServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * RulesServlet
 *
 * @author 谭文斌
 * @since 2023/9/5
 */
@WebServlet("/admin/rulesServlet")
public class RulesServlet extends HttpServlet {
    RulesService service =new RulesServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json; charset=utf8");
        PrintWriter out = resp.getWriter();

        int code = 1;
        String msg = "error";
        JSONObject jsonObject = new JSONObject();

        List<Rules> list=service.getList();
        if (list!=null){
            code=0;
            msg = "查询成功";
        }else {
            msg = "查询失败";
        }
        jsonObject.put("code",code);
        jsonObject.put("msg",msg);
        jsonObject.put("data",list);
        out.print(jsonObject.toString());
    }
}
